package cn.edu.sgu.www.mhxysy.service.partner.impl;

import cn.edu.sgu.www.mhxysy.dto.partner.PartnerSkillDTO;
import cn.edu.sgu.www.mhxysy.entity.partner.Partner;
import cn.edu.sgu.www.mhxysy.entity.partner.PartnerSkill;
import cn.edu.sgu.www.mhxysy.mapper.partner.PartnerMapper;
import cn.edu.sgu.www.mhxysy.mapper.partner.PartnerSkillMapper;
import cn.edu.sgu.www.mhxysy.service.partner.PartnerSkillService;
import cn.edu.sgu.www.mhxysy.vo.treegrid.PartnerSkillTreeGrid;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class PartnerSkillServiceImpl implements PartnerSkillService {

    private final PartnerSkillMapper partnerSkillMapper;
    private final PartnerMapper partnerMapper;

    @Autowired
    public PartnerSkillServiceImpl(PartnerSkillMapper partnerSkillMapper, PartnerMapper partnerMapper) {
        this.partnerSkillMapper = partnerSkillMapper;
        this.partnerMapper = partnerMapper;
    }

    @Override
    public void distribute(PartnerSkillDTO partnerSkillDTO) {
        Integer partnerId = partnerSkillDTO.getPartnerId();
        Integer type = partnerSkillDTO.getType();

        // 1、删除助战的技能
        QueryWrapper<PartnerSkill> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("partner_id", partnerId);
        queryWrapper.eq("type", type);

        Long count = partnerSkillMapper.selectCount(queryWrapper);

        if (count > 0) {
            partnerSkillMapper.delete(queryWrapper);
        }

        // 2、为助战重新分配技能
        PartnerSkill partnerSkill = new PartnerSkill();

        partnerSkill.setType(type);
        partnerSkill.setPartnerId(partnerId);

        // 便利助战技能类型ID列表，批量添加助战技能
        List<Integer> skillIds = partnerSkillDTO.getSkillIds();

        for (int i = 0; i < skillIds.size(); i++) {
            partnerSkill.setId(partnerSkill.uuid() + (i+1));
            partnerSkill.setSkillId(skillIds.get(i));

            partnerSkillMapper.insert(partnerSkill);
        }
    }

    @Override
    public void delete(Integer skillId, Integer partnerId) {
        QueryWrapper<PartnerSkill> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(
                skillId != null,
                "partner_id", skillId
        );
        queryWrapper.eq(
                partnerId != null,
                "skill_id", partnerId
        );

        partnerSkillMapper.delete(queryWrapper);
    }

    @Override
    public List<Integer> selectSkillIds(Integer type, Integer partnerId) {
        QueryWrapper<PartnerSkill> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("skill_id");

        queryWrapper.eq("type", type);
        queryWrapper.eq("partner_id", partnerId);

        List<PartnerSkill> list = partnerSkillMapper.selectList(queryWrapper);

        if (list.size() > 0) {
            Stream<PartnerSkill> stream = list.stream();

            return stream.map(new Function<PartnerSkill, Integer>() {
                @Override
                public Integer apply(PartnerSkill partnerSkill) {
                    return partnerSkill.getSkillId();
                }
            }).collect(Collectors.toList());
        }

        return new ArrayList<>();
    }

    @Override
    public List<PartnerSkillTreeGrid> listTree(Integer partnerId) {
        if (partnerId == null) {
            return new ArrayList<>();
        }

        // 1、初始化children
        // 基础技能
        PartnerSkillTreeGrid base = new PartnerSkillTreeGrid();

        base.setId(2023);
        base.setState("open");
        base.setName("基础技能");
        base.setChildren(partnerSkillMapper.selectByTypeAndPartnerId(partnerId, 0));

        // 特殊技能
        PartnerSkillTreeGrid special = new PartnerSkillTreeGrid();

        special.setId(2024);
        special.setState("open");
        special.setName("特殊技能");
        special.setChildren(partnerSkillMapper.selectByTypeAndPartnerId(partnerId, 1));

        List<PartnerSkillTreeGrid> children = new ArrayList<>(2);

        children.add(base);
        children.add(special);

        // 2、构建返回值对象
        Partner partner = partnerMapper.selectById(partnerId);

        PartnerSkillTreeGrid treeGrid = new PartnerSkillTreeGrid();

        treeGrid.setId(2025);
        treeGrid.setState("open");
        treeGrid.setChildren(children);

        if (partner != null) {
            treeGrid.setName(partner.getName());
        }

        List<PartnerSkillTreeGrid> treeGrids = new ArrayList<>();

        treeGrids.add(treeGrid);

        return treeGrids;
    }

}